package com.lfy.modules.blog.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.blog.domain.FeedbackEntity;
import com.lfy.modules.blog.domain.vo.feedback.FeedbackVO;
import com.lfy.modules.blog.service.FeedbackService;
import com.lfy.modules.blog.mapper.FeedbackMapper;
import com.lfy.modules.common.custom.constants.FeedbackEnum;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import com.lfy.modules.common.utils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【blog_feedback】的数据库操作Service实现
* @createDate 2024-05-14 17:44:28
*/
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, FeedbackEntity>
    implements FeedbackService{

    @Override
    public R<PageResultVO<FeedbackVO>> getFeedbackList(Integer pageSize, Integer pageNum,Long userId) {
        Page<FeedbackEntity> feedbackEntityPage = new Page<>(pageNum, pageSize);

        Page<FeedbackEntity> page = this.baseMapper.selectPage(feedbackEntityPage, new LambdaQueryWrapper<FeedbackEntity>().eq(FeedbackEntity::getTenantId,userId));
        List<FeedbackEntity> records =
                page.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return R.success(PageUtil.convert2PageEmptyResult(page, FeedbackVO.class));
        }
        List<FeedbackVO> result = records.stream().map(item -> {
            FeedbackVO feedbackVO = new FeedbackVO();
            BeanUtils.copyProperties(item, feedbackVO);
            feedbackVO.setText(item.getMsg());
            feedbackVO.setCreateTime(DateUtil.formatDateTime(item.getCreateTime()));
            return feedbackVO;
        }).collect(Collectors.toList());
        PageResultVO<FeedbackVO> feedbackVOPageResultVO = PageUtil.buildPageResultVO(page, result);
        return R.success(feedbackVOPageResultVO);
    }

    @Override
    public R<Void> deleteFeedback(Integer id) {
        UserEntity user = AuthHandlerInterceptor.authLocal.get();
        this.baseMapper.delete(new LambdaQueryWrapper<FeedbackEntity>().eq(FeedbackEntity::getId,id).eq(FeedbackEntity::getTenantId,user.getId()));
        return R.success();
    }

    @Override
    public R<Void> modifyFeedback(Integer id) {
        UserEntity user = AuthHandlerInterceptor.authLocal.get();
        FeedbackEntity feedbackEntity = new FeedbackEntity();
        feedbackEntity.setStatus(FeedbackEnum.SUCCESS.getCode());
        this.baseMapper.update(feedbackEntity, new LambdaQueryWrapper<FeedbackEntity>().eq(FeedbackEntity::getId,id).eq(FeedbackEntity::getTenantId,user.getId()));
        return R.success();

    }
}




